package com.demo01.demo.controller;

import com.demo01.demo.Repository.ManagerRepository;
import com.demo01.demo.entity.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/test")
public class ManagerHandler {
    @Autowired
    private ManagerRepository managerRepository;

    @GetMapping("/findAll/{page}/{size}")
    public Page<Manager> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return managerRepository.findAll(pageable);
    }

    @GetMapping("/sort/{page}/{size}")
    public Page<Manager> Sort(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Sort sort = Sort.by(Sort.Direction.ASC, "time");
        Pageable pageable = PageRequest.of(page - 1, size,sort);
        return managerRepository.findAll(pageable);
    }

    @GetMapping("/sortall")
    public List<Manager> Sort() {
        Sort sort = Sort.by(Sort.Direction.ASC, "time");
        return managerRepository.findAll(sort);
    }

    @PostMapping("/save")
    public String save(@RequestBody Manager manager) {
        if(findById(manager.getId())!=null){
            return "error";
        }
        else {
            Manager result = managerRepository.save(manager);
            if (result != null) {
                return "success";
            } else {
                return "error";
            }
        }
    }

    @GetMapping("/findById/{users_id}")
    public Manager findById(@PathVariable("users_id") String id) {
        if(!managerRepository.findById(id).isEmpty()) {
            return managerRepository.findById(id).get();
        }
        else return null;
    }

    @PostMapping("/search/{page}/{size}")
    public Page<Manager> search(@RequestBody Manager manager,@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        String[] sql = {"","","","",""};
        if (manager.getId()!= null) {
            sql[0] = manager.getId();
        }
        if (manager.getName() != null) {
            sql[1] = manager.getName();
        }
        if (manager.getTname() != null) {
            sql[2] = manager.getTname();
        }
        if (manager.getTime() != null) {
            //System.out.println(manager);
            String[] date = manager.getTime().split("\\.");
            sql[3] = date[0];
            sql[4] = date[1];
        }
        return managerRepository.find(sql[0],sql[1],sql[2],sql[3],sql[4],pageable);
    }

    @PostMapping("/searchall")
    public List<Manager> searchall(@RequestBody Manager manager) {
        String[] sql = {"","","","",""};
        if (manager.getId()!= null) {
            sql[0] = manager.getId();
        }
        if (manager.getName() != null) {
            sql[1] = manager.getName();
        }
        if (manager.getTname() != null) {
            sql[2] = manager.getTname();
        }
        if (manager.getTime() != null) {
            //System.out.println(manager);
            String[] date = manager.getTime().split("\\.");
            sql[3] = date[0];
            sql[4] = date[1];
        }
        return managerRepository.searchall(sql[0],sql[1],sql[2],sql[3],sql[4]);
    }

    @PutMapping("/update")
    public String update(@RequestBody Manager manager) {
        Manager result = managerRepository.save(manager);
        if (result != null) {
            return "success";
        } else {
            return "error";
        }
    }

    @DeleteMapping("/deleteById/{user_id}")
    public void deleteById(@PathVariable("user_id") String id) {
        managerRepository.deleteById(id);
    }

    @PostMapping("/Login")
    public String login(@RequestBody Manager manager) {
        if(!managerRepository.findByName(manager.getName()).isEmpty()) {
            Manager result = managerRepository.findByName(manager.getName()).get();
            if (result != null) {
                if (result.getPassword().equals(manager.getPassword())) {
                    return "success";
                } else {
                    return "pswerror";
                }
            } else {
                return "usernameerror";
            }
        }
        else {
            return "usernameerror";
        }
    }

    @GetMapping("/findall")
    public List<Manager> findall(){
        return managerRepository.findAll();
    }
}
